export default {
  transferParamsToUrl (data) {
    let dataStr = '?'
    Object.keys(data).forEach(key => {
      dataStr = dataStr + key + '=' + data[key] + '&'
    })
    dataStr = dataStr.substring(0, dataStr.lastIndexOf('&'))
    // console.log('urldataStr::' + dataStr)
    return dataStr
  },

  getValFromUrl (url, valName) {
    let query = ''
    if (url) {
      let lastIndexOfQuery = url.lastIndexOf('?')
      query = url.substring(lastIndexOfQuery + 1)
    } else {
      query = window.location.search.substring(1)  // 获取当前url ? 之后的内容
    }
    let vars = query.split('&')

    // 将vars存放的第一个字符串通过 = 符号拆开
    // 即，将 openid=student1 分成 openid 和 student 存放到pair字符串组中
    for (var i = 0; i < vars.length; i++) {
      var pair = vars[i].split('=')
      if (pair[0] === valName) {
        return (pair[1])
      } else {
        continue  // 如果这个pair[0]与variable不匹配，继续下一个
      }
    }
    return (false)
  },

  // 获取当前时间并格式化
  // 对Date的扩展，将 Date 转化为指定格式的String
  // 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符，
  // 年(y)可以用 1-4 个占位符，毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
  // 例子：
  // (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
  // (new Date()).Format("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18
  getCurrentTime () {
    let myDate = new Date()
    let year = myDate.getFullYear()    // 获取完整的年份(4位,1970-????)
    let month = myDate.getMonth() + 1       // 获取当前月份(0-11,0代表1月)
    if (month < 10) {
      month = '0' + month
    }
    let day = myDate.getDate()        // 获取当前日(1-31)
    if (day < 10) {
      day = '0' + day
    }
    let hour = myDate.getHours()       // 获取当前小时数(0-23)
    if (hour < 10) {
      hour = '0' + hour
    }
    let minute = myDate.getMinutes()     // 获取当前分钟数(0-59)
    if (minute < 10) {
      minute = '0' + minute
    }
    let second = myDate.getSeconds()     // 获取当前秒数(0-59)
    if (second < 10) {
      second = '0' + second
    }
    // let micro_second = myDate.getMilliseconds();    // 获取当前毫秒数(0-999)

    let time_string = year + '-' +
      month + '-' +
      day + 'T' +
      hour + ':' +
      minute + ':' +
      second
    return time_string
  },
  setDayCompareWithToday (numOfDay) {
    let now = new Date()
    let today = new Date(now.getFullYear(), now.getMonth(), now.getDate())
    let theDay_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * (numOfDay)
    let theDay = new Date()
    theDay.setTime(theDay_milliseconds)
    let strYear = theDay.getFullYear()
    let strDay = theDay.getDate()
    let strMonth = theDay.getMonth() + 1
    if (strMonth < 10) {
      strMonth = '0' + strMonth
    }
    if (strDay < 10) {
      strDay = '0' + strDay
    }
    let day = strYear + '-' + strMonth + '-' + strDay
    return day
  },
  transferTimeString (timeString) {
    //日期字符串格式  2020-03-02T20:30:14.715037
    // 获取一直到分钟的字符串
    let after = timeString.slice(0, timeString.lastIndexOf(':'))
    return after
  },

  randomString (len) {
    len = len || 19;
    let $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
    let maxPos = $chars.length;
    let randomStr = '';
    for (let i = 0; i < len; i++) {
      randomStr += $chars.charAt(Math.floor(Math.random() * maxPos));
    }
    randomStr += new Date().getTime();
    return randomStr;
  }
}
